package spring.chatroom.java_chatroom.common.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.resource.NoResourceFoundException;
import spring.chatroom.java_chatroom.pojo.response.Response;

import java.util.Map;

@RestControllerAdvice
@Slf4j
@ControllerAdvice
public class ExceptionAdvice {
    private final View error;

    public ExceptionAdvice(View error) {
        this.error = error;
    }

    @ExceptionHandler(NumberFormatException.class)
    public Response handleNumberFormatException(NumberFormatException e) {
        log.error("参数格式不对：{}", e.getMessage(), e);
        return Response.success(Map.of("status", "fail", "msg", "错误的格式：" + e.getMessage()));
    }

    @ExceptionHandler(NoResourceFoundException.class)
    public Response handleNoResourceFoundException(NoResourceFoundException e) {
        log.info("NoResourceFoundException 没有该资源 {}", e.getMessage());
        return Response.success(Map.of("status", "fail", "msg", e.getMessage()));
    }

    @ExceptionHandler(Exception.class)
    public Response handleException(Exception e){
        log.error("捕捉到异常：{}", e.getMessage(), e);
        return Response.success(Map.of("status", "fail", "msg", e.getMessage()));
    }
}
